#!/usr/bin/python

import random
from zplot import *
import xml.etree.ElementTree as ET
import parse

class MapVis(object):
    def __init__(self, path):
        self.data = parse.Parse(path)

    def plot(self):
        xmin = float('Inf')
        xmax = -xmin
        ymin = float('Inf')
        ymax = -ymin

        for w in self.data.ways:
          for n in w.nodes:
            xmin = min(xmin, n[0])
            xmax = max(xmax, n[0])
            ymin = min(ymin, n[1])
            ymax = max(ymax, n[1])

        xrang = [xmin, xmax]
        yrang = [ymin, ymax]

        c = postscript(title='map.eps', dimensions=[400,400])
        cr = canvasregion(canvas=c, coord=[10,10], dimensions=[380,380])
        d = drawable(canvasreg=cr, xrange=xrang, yrange=yrang)
        cr.border()

        for w in self.data.ways:
            for i in range(len(w.nodes)-1):
                n1 = w.nodes[i]
                n2 = w.nodes[i+1]
                coord = d.map([[n1[0], n1[1]], [n2[0], n2[1]]])
                c.line(coord=coord, linecolor='0,0,0')

        for w in self.data.ways:
            if 'John Nolen' in w.name:
              print w.toString()
              color = '%f,%f,%f' % (
                  random.random(), random.random(), random.random())
              for i in range(len(w.nodes)-1):
                  n1 = w.nodes[i]
                  n2 = w.nodes[i+1]
                  coord = d.map([[n1[0], n1[1]], [n2[0], n2[1]]])
                  c.line(coord=coord, linecolor=color)

        c.render()

def main():
    mv = MapVis('data/lowell.xml')
    mv.plot()


if __name__ == '__main__':
    main()
